package com.tgu.home_action.service.utils;

import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;

@Service
public class StringService {
    /**
     * 生成不重复的随机字符串
     */
    public static String getRandomString() {
        return UUID.randomUUID().toString();
    }

    /**
     * 获取gis（mysql位置存储格式
     */
    public static String getGis(Double longitude, Double latitude) {
        return "POINT(" + longitude + " " + latitude + ")";
    }

    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 获取两地间的距离
     */
    public static double getDistance(double lat1, double lng1, double lat2,
                                     double lng2) {
        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double a = radLat1 - radLat2;
        double b = rad(lng1) - rad(lng2);
        double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(b / 2), 2)));
        s = s * EARTH_RADIUS;
        s = Math.round(s * 10000d) / 10000d;
        s = s * 1000;
        return s;
    }

    /**
     * 拼接Url和参数
     */
    public static String getUrl(String url, Map<String, Object> param) {
        Iterator<Map.Entry<String, Object>> it = param.entrySet().iterator();
        int i = 0;
        StringBuilder urlBuilder = new StringBuilder(url);
        while (it.hasNext()) {
            Map.Entry<String, Object> entry = it.next();
            if (i == 0) {
                urlBuilder.append("?").append(entry.getKey()).append("=").append(entry.getValue());
            } else {
                urlBuilder.append("&").append(entry.getKey()).append("=").append(entry.getValue());
            }
            i++;
        }
        return urlBuilder.toString();
    }

    /**
     * 为手机号添加86前缀
     */
    public static String phoneAdd86(String phone) {
        return "+86" + phone;
    }

    /**
     * 获取6位短信验证码
     */
    public static String getCode() {
        int code = (int) ((Math.random() * 9 + 1) * 100000);
        return String.valueOf(code);
    }

    /**
     * 将日期对象转换为指定的日期字符串
     *
     * @param date   传入的日期对象
     * @param format 格式
     * @return 日期字符串
     */
    public static String dateToString(Date date, String format) {
        String string = "";
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        if (date != null) {
            string = simpleDateFormat.format(date);
        }
        return string;
    }

    /**
     * 将日期字符串转换为一个日期对象
     *
     * @param datestr 日期字符串
     * @param format  格式
     * @return Date
     * @throws ParseException
     */
    public static Date stingToDate(String datestr, String format) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);

        return simpleDateFormat.parse(datestr);
    }

}
